package com.example.controller;

import com.example.entity.Menu;
import com.example.entity.Orders;
import com.example.service.MenuService;
import com.example.util.JwtUtils;
import com.example.util.ResultCodeUtil;
import com.example.util.ResultUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author : chenjianhua
 * @date : 17:04 2021/2/23
 */

@CrossOrigin
@RestController
public class MenuController  extends BaseController{

    @Autowired
    private MenuService menuService;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    UserController userController;



    @PostMapping("/insert")
    public ResultUtil insert(@RequestBody Menu menu){
        try {
            menuService.insert(menu);
            System.out.println(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil(ResultCodeUtil.FAIL,e.getMessage());
        }
        return new ResultUtil(ResultCodeUtil.SUCCESS);
    }


    @PostMapping("/qryInfoMenu")
    public ResultUtil qryInfo(){
        List<Menu> menuList = null;
        try {
            menuList = menuService.qryInfo();
//            System.out.println(menuList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultUtil(ResultCodeUtil.SUCCESS,menuList);
    }

    @PostMapping("/updateMenuInfoById")
    public ResultUtil updateMenuInfoById(@RequestBody Menu menu) {
        try {
            menuService.updateMenuInfoById(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil(ResultCodeUtil.FAIL);
        }
        return new ResultUtil(ResultCodeUtil.SUCCESS);
    }

    // 按照菜品或菜品种类查找
    @GetMapping("/qryMenuByName/{keyword}")
    public ResultUtil qryMenuByName(@PathVariable("keyword") String keyword){
        keyword = keyword.trim().replace(" ","");
        List<Menu> menuList = null;
        try {
            menuList = menuService.qryMenuByName(keyword);
            System.out.println("按照菜品或种类查找："+menuList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultUtil(ResultCodeUtil.SUCCESS,menuList);
    }


    @PostMapping("/deleteMenuInfoById")
    public ResultUtil deleteMenuInfoById(@RequestBody Map<String, Integer> map) {
        Integer foodid = map.get("foodid");
        try {
            menuService.deleteMenuInfoById(foodid);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil(ResultCodeUtil.FAIL);
        }
        return new ResultUtil(ResultCodeUtil.SUCCESS);
    }

    @PostMapping("/click")
    public ResultUtil clickById(@RequestBody Map<String, Integer> map){
        Integer foodid = map.get("foodid");
        try {
            Menu menu = menuService.qryMenuInfoById(foodid);
            menuService.clickById(
                    new Orders().setUserid(this.userid)
                                .setFoodid(foodid)
                                .setPrice(menu.getPrice()));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil(ResultCodeUtil.FAIL);
        }
        return new ResultUtil(ResultCodeUtil.SUCCESS);

    }

    @PostMapping("/qryMenuInfoById")
    public ResultUtil qryMenuInfoById(@RequestBody Map<String, Integer> map){
        Integer foodid = map.get("foodid");
        Menu menu = null;
        try {
            menu = menuService.qryMenuInfoById(foodid);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil(ResultCodeUtil.FAIL);
        }
        return new ResultUtil(ResultCodeUtil.SUCCESS, menu);
    }

}
